package leetcode.L160;

import java.util.HashMap;

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
      val = x;
      next = null;
    }
}
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA==null||headB==null) {
            return null;
        }
        //可以换成set
        HashMap<ListNode,ListNode> map=new HashMap<ListNode, ListNode>();
        map.put(headA, headA);
        while (headA.next != null) {
            headA = headA.next;
            map.put(headA, headA);
        }
        while (headB != null) {
            if (map.containsKey(headB)) {
                return headB;
            }
            headB = headB.next;
        }
        return null;
    }
}